﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using AutoStar;

namespace AutoStar
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void txtKorisnickoIme_Enter(object sender, EventArgs e)
        {
            if (txtKorisnickoIme.Text == "Korisničko ime")
            {
                txtKorisnickoIme.Text = "";
                txtKorisnickoIme.ForeColor = Color.Black;
            }
        }

        private void txtLozinka_Enter(object sender, EventArgs e)
        {
            if (txtLozinka.Text == "Lozinka")
            {
                txtLozinka.Text = "";
                txtLozinka.PasswordChar = '*';
                txtLozinka.ForeColor = Color.Black;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            testLogin();
        }

        private void resetujTxtKorisnickoIme()
        {
            txtKorisnickoIme.Text = "Korisničko ime";
            txtKorisnickoIme.ForeColor = SystemColors.ControlDark;
        }

        private void resetujTxtLozinka()
        {
            txtLozinka.Text = "Lozinka";
            txtLozinka.PasswordChar = '\0';
            txtLozinka.ForeColor = SystemColors.ControlDark;
        }

        private void testLogin()
        {
            try
            {
                CalculateMD5Hash hashCalculator = new CalculateMD5Hash();
                string hashLozinke = hashCalculator.GetMD5Hash(txtLozinka.Text, 50);
                MySqlConnection conn = ConnectionPool.checkOutConnection();
                AutoStarBaza context = new AutoStarBaza(conn);
                var korisnici = from k in context.korisnik
                                where k.userName == txtKorisnickoIme.Text && k.password == hashLozinke
                                select k;
                if (korisnici.Count() != 0)
                {
                    Form1 glavnaForma = new Form1(this, korisnici.First());
                    glavnaForma.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Netačno korisničko ime ili lozinka!", "Greška!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    resetujTxtLozinka();
                    resetujTxtKorisnickoIme();
                    //txtKorisnickoIme.Text = hashLozinke;
                }
                ConnectionPool.checkInConnection(conn);
            }
            catch (MySqlException ex)
            {
                MessageBox.Show("Nije moguce povezati se sa bazom podataka. Program ce se zavrsiti. Detalji: " + ex.Message, "Problem u ucitavanju baze podataka", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }
            finally 
            { }
        }

        private void txtKorisnickoIme_Leave(object sender, EventArgs e)
        {
            if (txtKorisnickoIme.Text == "") resetujTxtKorisnickoIme();
        }

        private void txtLozinka_Leave(object sender, EventArgs e)
        {
            if (txtLozinka.Text == "") resetujTxtLozinka();
        }

        private void txtLozinka_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) testLogin();
        }

        private void txtKorisnickoIme_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) testLogin();
        }
    }
}
